package com.xk857.simpleeffect.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.activity.UpdateMatterActivity;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.entity.Matter;
import com.xk857.simpleeffect.utils.OkHttpUtils;

import java.lang.reflect.Type;
import java.util.List;
import java.util.function.Consumer;

public class ListMatterItemAdapter extends RecyclerView.Adapter<ListMatterItemAdapter.ViewHolder> {

    private final Context mContext;
    private final List<Matter> matterList;
    private final Consumer<Boolean> consumer;
    public static final int[] iconDraws = {R.drawable.todo0s, R.drawable.todo1s, R.drawable.todo2s, R.drawable.todo3s, R.drawable.todo4s, R.drawable.todo5s};


    public ListMatterItemAdapter(Context mContext, List<Matter> matterList, Consumer<Boolean> consumer) {
        this.mContext = mContext;
        this.matterList = matterList;
        this.consumer = consumer;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_matter_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int icon = iconDraws[Integer.parseInt(matterList.get(position).getIcon())];
        holder.iconView.setImageDrawable(AppCompatResources.getDrawable(mContext, icon));
        holder.titleView.setText(matterList.get(position).getTitle());
        holder.mainLayout.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, UpdateMatterActivity.class);
            intent.putExtra("id", matterList.get(position).getId());
            mContext.startActivity(intent);
        });
        // 删除
        holder.btnDelete.setOnClickListener(v -> {
            Type type = new TypeToken<Result<Boolean>>() {}.getType();
            String url = OkHttpUtils.BASE_URL + "/api/v1/matter/" + matterList.get(position).getId();
            OkHttpUtils.getInstance().delete(mContext, url, type, null,false, consumer);
        });
    }

    @Override
    public int getItemCount() {
        return matterList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iconView;
        TextView titleView;
        LinearLayout mainLayout;
        Button btnDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.iconView = itemView.findViewById(R.id.icon_view);
            this.titleView = itemView.findViewById(R.id.title_text);
            this.mainLayout = itemView.findViewById(R.id.main);
            this.btnDelete = itemView.findViewById(R.id.btnDelete);
        }
    }
}
